(ns first-clojure.routes.guestbook
  (:require
    [first-clojure.db.db-guestbook :as db]
    [first-clojure.validation :refer [validate-message]]
    [ring.middleware.cookies :as cookies]
    [clj-http.client :as http]
    [clojure.java.io :as io]
    [taoensso.carmine :as car :refer (wcar)]
    [first-clojure.db.redis :as redis]))

(defn guestbook-routes []
  ["/guestbooks"
   {:swagger {:tags ["guestbook"]}}

   [""
    {:get  {:summary    "根据id获取guestbook"
            :parameters {:query {:id int?}}
            :handler    (fn [{{{:keys [id]} :query} :parameters}]
                          {:status 200
                           :body   {:data (db/get-guestbook {:id id})}
                           :cookies {:my-cookie "123123123"}})}
     :post {:summary    "新建guestbook消息"
            :parameters {:body {:name string?, :message string?}}
            :handler    (fn [{:keys [body-params]}]
                          (println "-------------")
                          (if-let [errors (validate-message body-params)]
                            {:status 200
                             :body   {:code    10010
                                      :message (get-in errors [:message])}}
                            (let [result (assoc body-params :timestamp (java.util.Date.)
                                                            :id (str (java.util.UUID/randomUUID)))]
                              (db/create-guestbook! result)
                              {:status 200
                               :body   (dissoc result :timestamp)})))}}]

   #_["/add"
      {:post {:summary    "新建"
              :parameters {:body {:name string?, :message string?}}
              :handler    (fn [{{{:keys [name, message]} :body} :parameters}]
                            {:code 10020
                             :body {:total (db/create-guestbook! {:id        (str (uuid/v1)),
                                                                  :name      name,
                                                                  :message   message,
                                                                  :timestamp (java.util.Date.)})}})}}]
   ["/page"
    {:get {:summary    "分页获取guestbooks"
           :parameters {:query {:page int?
                                :size int?}}
           :handler    (fn [{{{:keys [page, size]} :query} :parameters}]
                         {:status 200
                          :body   {:data  (db/get-guestbooks {:page (* page size), :offset size})
                                   :total (:total (db/select-count))}})}}]

   ["/pdf"
    {:get {:summary    "一个获取pdf的例子"
           :handler    (fn [_]
                         {:status 200
                          :headers {"Content-Typ" "application/pdf"
                                    "Access-Control-Allow-Origin" "*"}
                          :body (-> "public/pdf/responseTypes.pdf"
                                    (io/resource)
                                    (io/input-stream))})}}]

   ["/http/get"
    {:get {:summary "一个获取httpclient调用的例子"
           :handler (fn [_]
                      {:status 200
                       :body   {:code 1
                                :message "请求成功"
                                :data (:body
                                        (http/get "http://cdn.imgs.3vyd.com/xh/admin/test.json" {:as :json}))}})}}]

   ["/http/post/json"
    {:get {:summary "以json body提交"
           :handler (fn [_]
                      {:status 200
                       :body   {:code 1
                                :message "请求成功"
                                :data (:body
                                        (http/post "http://localhost:8185/management/public/doctor"
                                          {:form-params
                                            {:mobile "15092107093",
                                              :nickName "marvin.ma",
                                              :name "marvin",
                                              :openid "8"}
                                           :content-type :json}))}})}}]

   ["/http/post/form"
    {:get {:summary "以form body提交"
           :handler (fn [_]
                      {:status 200
                       :body   {:code 1
                                :message "请求成功"
                                :data (:body
                                        (http/post "http://localhost:8185/management/oauth2/token"
                                          {:form-params
                                           {:username "test",
                                            :password "test123",
                                            :client_id "management-Client",
                                            :grant_type "password"}}))}})}}]

   ["/redis/set"
    {:get {:summary    "set redis by key"
           :parameters {:query {:key string?
                                :value string?}}
           :handler    (fn [{{{:keys [key, value]} :query} :parameters}]
                         {:status 200
                          :body   (redis/wcar*
                                    (car/ping)
                                    (car/set key value))})}}]

   ["/redis/get"
    {:get {:summary    "set redis by key"
           :parameters {:query {:key string?}}
           :handler    (fn [{{{:keys [key]} :query} :parameters}]
                         {:status 200
                          :body   {:data {key (redis/wcar*
                                                (car/get key))} }})}}]
   ["/path/:id"
    {:post {:summary    "路径上传参"
            :parameters {:path  {:id int?}
                         :query {:name string?}
                         :body  {:message string?}}
            :handler    (fn [{:keys [path-params query-params body-params]}]
                          {:status 200
                           :body   {:data (str "path params: " path-params
                                               "\nquery params: " query-params
                                               "\nbody params: " body-params)}})}}]])


